package org.jboss.jawabot.mail;


import org.apache.commons.lang.StringUtils;
import org.jboss.jawabot.JawaBot;
import org.jboss.jawabot.JawaBotApp;
import org.jboss.jawabot.MailData;
import org.jboss.jawabot.config.beans.ConfigBean;
import org.jboss.jawabot.ex.JawaBotException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 *  @author Ondrej Zizka, ozizka at redhat.com
 * 
 *  TODO: Move to core; Use CDI.
 */
public class MailSender {
    private static final Logger log = LoggerFactory.getLogger( MailSender.class );
    
    
    //private ConfigBean cnf;
    private JawaBot jawaBot;

    public MailSender( JawaBot jawaBot ) {
        this.jawaBot = jawaBot;
    }
    

    /**
     * Sends a mail announcement about user's action.
     * TODO: Move to some MailService.
     */
    public void sendMail( String fromUser, MailData mail ) throws JawaBotException {

        ConfigBean cnf = this.jawaBot.getConfig();

        log.debug( String.format( "Sending mail: host %s, to %s, from %s <%s>",
            cnf.settings.smtpHost,
            cnf.settings.announceEmailTo,
            fromUser,
            cnf.settings.announceEmailFrom ) );

        String messageBody = (StringUtils.isBlank( mail.messageBody ) ? "" : mail.messageBody + "\n\n");
        messageBody += "Possible sender's e-mail: " + fromUser + "@redhat.com\nThis message was "
            + "generated by JawaBot " + JawaBotApp.VERSION + ".\n" + JawaBotApp.PROJECT_DOC_URL;
        mail.messageBody = messageBody;
        mail.fromName = fromUser + " via JawaBot";

        this.jawaBot.getMailUtils().sendMail( mail );
    }    


}// class
